package com.test.ml.yqs.service;


import com.alibaba.fastjson.JSON;
import com.test.ml.config.RestTemplateConfig;
import com.test.ml.yqs.entity.BetCode;
import com.test.ml.yqs.entity.OpenCode;
import com.test.ml.yqs.entity.SystemVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 长龙计划
 *
 * @author 马亮
 * 自动跟长龙
 */
@Service
@Import(value = {RestTemplateConfig.class})
public class BigLongPlan {

    @Autowired
    private  RestTemplate restTemplate;

    @Value("${cqssc.openresult.url}")
    private static String openresult;
    /**
     * 存入大小，单双相关信息 0 小 1 大 , 0双，1单
     */
    private static String pid_sq;//上期期数

    private static Double totalWinMoney = 0D;

    private static Double bigloseMoney = 0D;

    private static Double bigWinMeney = 0D;

    private static Map<String, Integer> lastBetMoneyMap = new HashMap<>();
    private static Map<String, Integer> bigLoseMap = new HashMap<>();
    @Autowired
    private  CqsscBetService betService;

    /**
     * 获取开奖结果，分析结果
     * 》=3开始投 ，倍率： 3 ：1 ， 4:3 ，  5： 6，   6 ：  15  7： 30
     * 高风险：biglong 》=12的开始投，倍率： 12 ： 10， 13:30  ， 14 ： 60 ， 15:150， 16， 300
     * 低风险： biglog>=14 开始投，倍率： 14:10 15： 30 16： 60 17:150
     *
     * @param money 起始金额
     * @return 返回投注是否成功
     */
    public boolean StartGame(int money) {

        List<OpenCode> codelist = getopencode();
        String pid = null;
        if (codelist != null) {
            pid = codelist.get(codelist.size() - 1).getPerioud();
        }
         /*如果上期和最后不等，说明结果更新的，否则说明这期已经投了*/
        if (pid_sq == null || !pid_sq.equals(pid)) {
            SystemVariable.setPid(pid);
            Map<String, Integer> codelong = getBigLogMap(codelist);
           /* 计算投注*/
            Map<String, Integer> betMoneyMap = getThisBetMoneyMap(money, codelong);
            Double thisTimeloseMoney = 0D;
            Double thisTimeWinMoney = 0D;
            if (CollectionUtils.isEmpty(betMoneyMap)) {
                if (!CollectionUtils.isEmpty(lastBetMoneyMap)) {
                    calWinMoney(thisTimeloseMoney, thisTimeWinMoney);
                }
            } else {
                boolean flag = betService.postbetMap(betMoneyMap);
                if (!flag) {
                    return flag;
                }
            /*计算输赢*/
                if (lastBetMoneyMap.keySet().size() > 0) {
                    for (String key : betMoneyMap.keySet()) {
                        if (lastBetMoneyMap.get(key) != null) {
                            thisTimeloseMoney += lastBetMoneyMap.get(key);
                            lastBetMoneyMap.remove(key);
                        }
                    }
                    calWinMoney(thisTimeloseMoney, thisTimeWinMoney);

                } else {
                    System.out.println("上期未投注。。。");
                }
            }
            System.out.println("this pid : " + pid);
            System.out.println("the lastBetMap : " + JSON.toJSON(lastBetMoneyMap).toString());
            System.out.println("the this betmoneymap :" +JSON.toJSON(betMoneyMap).toString() );
            System.out.println("++++++++++++++++++++++++++++++++++");
            lastBetMoneyMap = betMoneyMap;
            pid_sq = pid;
        } else {
            return true;
        }
        return true;
    }

    private void calWinMoney(Double thisTimeloseMoney, Double thisTimeWinMoney) {
        for (String key : bigLoseMap.keySet()) {
            thisTimeloseMoney += bigLoseMap.get(key);
            lastBetMoneyMap.remove(key);
        }
        bigLoseMap.clear();
        for (String key : lastBetMoneyMap.keySet()) {
            thisTimeWinMoney += lastBetMoneyMap.get(key) * 0.993;
        }
        Double sum = thisTimeWinMoney - thisTimeloseMoney;

        totalWinMoney += sum;
        if (bigloseMoney > totalWinMoney) {
            bigloseMoney = totalWinMoney;
        }
        if (bigWinMeney < totalWinMoney) {
            bigWinMeney = totalWinMoney;
        }
        System.out.println("this time win :" + sum);
        System.out.println("total win money: " + totalWinMoney);
        System.out.println("big win money :" + bigWinMeney);
        System.out.println("big lose money : " + bigloseMoney);
    }

    static final  Integer  startnu=0;
    /**
     * 获取这期要投注的东西
     *跟6大
     * @param money
     * @param codelong
     * @return
     */
    private Map<String, Integer> getThisBetMoneyMap(int money, Map<String, Integer> codelong) {
        Map<String, Integer> betMoneyMap = new HashMap<>();
        for (String key : codelong.keySet()) {
            switch (codelong.get(key)){
                case 3:
                    betMoneyMap.put(key, 1 * money);
                    break;
                case 4:
                    betMoneyMap.put(key, 2 * money);
                    break;
                case 5:
                    betMoneyMap.put(key, 1 * money);
                    break;
                case 6:
                    betMoneyMap.put(key, 3 * money);
                    break;
                case  7:
                    betMoneyMap.put(key, 6 * money);
                    break;
                case 8:
                    betMoneyMap.put(key, 15 * money);
                    break;
                case 9:
                    bigLoseMap.put(changeKey(key), 15 * money);
                    break;
                case 15:
                    betMoneyMap.put(key, 60 * money);
                case 16:
                    betMoneyMap.put(key, 150 * money);
                    break;
                case 17:
                    betMoneyMap.put(key, 300 * money);
                    break;
                case 18:
                    bigLoseMap.put(changeKey(key), 300 * money);
                    break;
            }
        }
        Map<String, Integer> realbetMoneyMap = new HashMap<>();
        for (String key : betMoneyMap.keySet()) {
            realbetMoneyMap.put(changeKey(key), betMoneyMap.get(key));
        }
        return realbetMoneyMap;
    }


    /**
     * 获取这期要投注的东西
     *跟7大
     * @param money
     * @param codelong
     * @return
     */
    private Map<String, Integer> getThisBetMoneyMapPlan2(int money, Map<String, Integer> codelong) {
        Map<String, Integer> betMoneyMap = new HashMap<>();
        for (String key : codelong.keySet()) {
            if (codelong.get(key).equals(3)) {
                betMoneyMap.put(key, 1 * money);
            } else if (codelong.get(key).equals(4)) {
                betMoneyMap.put(key, 3 * money);
            } else if (codelong.get(key).equals(5)) {
                betMoneyMap.put(key, 6 * money);
            } else if (codelong.get(key).equals(6)) {
                betMoneyMap.put(key, 15 * money);
            } else if (codelong.get(key).equals(7)) {
                betMoneyMap.put(key, 50 * money);
            } else if (codelong.get(key).equals(7)) {
                bigLoseMap.put(changeKey(key), 50 * money);
            }
            else if (codelong.get(key).equals(14)) {
                betMoneyMap.put(key, 60 * money);
            } else if (codelong.get(key).equals(15)) {
                betMoneyMap.put(key, 150 * money);
            } else if (codelong.get(key).equals(16)) {
                betMoneyMap.put(key, 300 * money);
            } else if (codelong.get(key).equals(17)) {
                bigLoseMap.put(changeKey(key), 300 * money);
            }
        }
        Map<String, Integer> realbetMoneyMap = new HashMap<>();
        for (String key : betMoneyMap.keySet()) {
            realbetMoneyMap.put(changeKey(key), betMoneyMap.get(key));
        }
        return realbetMoneyMap;
    }


    /**
     * 获取这期要投注的东西
     * 4期一轮 没轮*3
     *小爆发玩法
     * @param money
     * @param codelong
     * @return
     */
    private Map<String, Integer> getThisBetMoneyMapPlanbom(int money, Map<String, Integer> codelong, Integer bei) {
        Map<String, Integer> betMoneyMap = new HashMap<>();
        for (String key : codelong.keySet()) {
            switch (codelong.get(key)) {
                case 3:
                    betMoneyMap.put(key, 1 * money);
                    break;
                case 4:
                    betMoneyMap.put(key, 2 * money);
                    break;
                case 5:
                    betMoneyMap.put(key, 3 * money);
                    break;
                case 6:
                    betMoneyMap.put(key, 6 * money);
                    break;
                case 7:
                    betMoneyMap.put(key, 15 * money);
                    break;
                case 8:
                    betMoneyMap.put(key, 1 * money * bei);
                    break;
                case 9:
                    betMoneyMap.put(key, 3 * money * bei);
                    break;
                case 10:
                    betMoneyMap.put(key, 6 * money * bei);
                    break;
                case 11:
                    betMoneyMap.put(key, 15 * money * bei);
                    break;
                case 12:
                    betMoneyMap.put(key, 1 * money * bei * bei);
                    break;
                case 13:
                    betMoneyMap.put(key, 3 * money * bei * bei);
                    break;
                case 14:
                    betMoneyMap.put(key, 6 * money * bei * bei);
                    break;
                case 15:
                    betMoneyMap.put(key, 15 * money * bei * bei);
                    break;
                case 16:
                    betMoneyMap.put(key, 1 * money * bei * bei * bei);
                    break;
                case 17:
                    betMoneyMap.put(key, 3 * money * bei * bei * bei);
                    break;
                case 18:
                    betMoneyMap.put(key, 6 * money * bei * bei * bei);
                    break;
                case 19:
                    betMoneyMap.put(key, 15 * money * bei * bei * bei);
                    break;
                case 20:
                    betMoneyMap.put(key, 1 * money * bei * bei * bei * bei);
                    break;
                case 21:
                    betMoneyMap.put(key, 3 * money * bei * bei * bei * bei);
                    break;
                case 22:
                    betMoneyMap.put(key, 6 * money * bei * bei * bei * bei);
                    break;
                case 23:
                    betMoneyMap.put(key, 15 * money * bei * bei * bei * bei);
                    break;
                case 24:
                    bigLoseMap.put(changeKey(key), 15 * money * bei * bei * bei * bei);
                    break;
            }
        }
        Map<String, Integer> realbetMoneyMap = new HashMap<>();
        for (String key : betMoneyMap.keySet()) {
            realbetMoneyMap.put(changeKey(key), betMoneyMap.get(key));
        }
        return realbetMoneyMap;
    }


    /**
     * 获取这期要投注的东西
     * 土豪玩法
     *
     * @param money
     * @param codelong
     * @return
     */
    private Map<String, Integer> getThisBetMoneyMapPlanTuHao(int money, Map<String, Integer> codelong) {
        Map<String, Integer> betMoneyMap = new HashMap<>();
        for (String key : codelong.keySet()) {
            switch (codelong.get(key)) {
                case 5:
                    betMoneyMap.put(key, 1 * money);
                    break;
                case 6:
                    betMoneyMap.put(key, 2 * money);
                    break;
                case 7:
                    betMoneyMap.put(key, 6 * money);
                    break;
                case 8:
                    betMoneyMap.put(key, 16 * money);
                    break;
                case 9:
                    betMoneyMap.put(key, 44 * money);
                    break;
                case 10:
                    betMoneyMap.put(key, 20 * money);
                case 11:
                    betMoneyMap.put(key, 50 * money);
                case 12:
                    betMoneyMap.put(key, 100 * money);
                case 13:
                    betMoneyMap.put(key, 200 * money);
                    break;
                case 14:
                    betMoneyMap.put(key, 328 * money);
                    break;
                case 15:
                    betMoneyMap.put(key, 896 * money);
                    break;
                case 16:
                    betMoneyMap.put(key, 2448 * money);
                    break;
                case 17:
                    betMoneyMap.put(key, 6688 * money);
                    break;
                case 18:
                    betMoneyMap.put(key, 18272 * money);
                    break;
                case 19:
                    betMoneyMap.put(key, 49920 * money);
                    break;
                case 20:
                    betMoneyMap.put(key, 136384 * money);
                    break;
                case 21:
                    bigLoseMap.put(changeKey(key), 136384 * money);
                    System.out.print("天要亡你，可以去跳楼了");
                    break;

            }
        }
        Map<String, Integer> realbetMoneyMap = new HashMap<>();
        for (String key : betMoneyMap.keySet()) {
            realbetMoneyMap.put(changeKey(key), betMoneyMap.get(key));
        }
        return realbetMoneyMap;
    }


    /**
     * 获取这期要投注的东西
     * 贫民版土豪玩法
     *
     * @param money
     * @param codelong
     * @return
     */
    private Map<String, Integer> getThisBetMoneyMapPlanSmallBom(int money, Map<String, Integer> codelong) {
        Map<String, Integer> betMoneyMap = new HashMap<>();
        for (String key : codelong.keySet()) {
            switch (codelong.get(key)) {
                case 11:
                    betMoneyMap.put(key, 1 * money);
                    break;
                case 12:
                    betMoneyMap.put(key, 2 * money);
                    break;
                case 13:
                    betMoneyMap.put(key, 6 * money);
                    break;
                case 14:
                    betMoneyMap.put(key, 16 * money);
                    break;
                case 15:
                    betMoneyMap.put(key, 44 * money);
                    break;
                case 16:
                    betMoneyMap.put(key, 120 * money);
                case 17:
                    betMoneyMap.put(key, 200 * money);
                case 18:
                    betMoneyMap.put(key, 500 * money);
//                case 11:
//                    betMoneyMap.put(key, 200 * money);
//                    break;
//                case 12:
//                    betMoneyMap.put(key, 328 * money);
//                    break;
//                case 13:
//                    betMoneyMap.put(key, 896 * money);
//                    break;
//                case 14:
//                    betMoneyMap.put(key, 2448 * money);
//                    break;
//                case 15:
//                    betMoneyMap.put(key, 6688 * money);
//                    break;
//                case 16:
//                    betMoneyMap.put(key, 18272 * money);
//                    break;
//                case 17:
//                    betMoneyMap.put(key, 49920 * money);
//                    break;
//                case 18:
//                    betMoneyMap.put(key, 136384 * money);
//                    break;
                case 19:
                    bigLoseMap.put(changeKey(key), 500 * money);
                    System.out.print("天要亡你，可以去跳楼了");
                    break;

            }
        }
        Map<String, Integer> realbetMoneyMap = new HashMap<>();
        for (String key : betMoneyMap.keySet()) {
            realbetMoneyMap.put(changeKey(key), betMoneyMap.get(key));
        }
        return realbetMoneyMap;
    }

    private String changeKey(String key) {
        String relkey = null;
        switch (key) {
            case BetCode.Q1_DAN:
                relkey = BetCode.Q1_SHUANG;
                break;
            case BetCode.Q1_SHUANG:
                relkey = BetCode.Q1_DAN;
                break;
            case BetCode.Q1_MAX:
                relkey = BetCode.Q1_MIN;
                break;
            case BetCode.Q1_MIN:
                relkey = BetCode.Q1_MAX;
                break;

            case BetCode.Q2_DAN:
                relkey = BetCode.Q2_SHUANG;
                break;
            case BetCode.Q2_SHUANG:
                relkey = BetCode.Q2_DAN;
                break;
            case BetCode.Q2_MAX:
                relkey = BetCode.Q2_MIN;
                break;
            case BetCode.Q2_MIN:
                relkey = BetCode.Q1_MAX;
                break;
            case BetCode.Q3_DAN:
                relkey = BetCode.Q3_SHUANG;
                break;
            case BetCode.Q3_SHUANG:
                relkey = BetCode.Q3_DAN;
                break;
            case BetCode.Q3_MAX:
                relkey = BetCode.Q3_MIN;
                break;
            case BetCode.Q3_MIN:
                relkey = BetCode.Q3_MAX;
                break;


            case BetCode.Q4_DAN:
                relkey = BetCode.Q4_SHUANG;
                break;
            case BetCode.Q4_SHUANG:
                relkey = BetCode.Q4_DAN;
                break;
            case BetCode.Q4_MAX:
                relkey = BetCode.Q4_MIN;
                break;
            case BetCode.Q4_MIN:
                relkey = BetCode.Q4_MAX;
                break;

            case BetCode.Q5_DAN:
                relkey = BetCode.Q5_SHUANG;
                break;
            case BetCode.Q5_SHUANG:
                relkey = BetCode.Q5_DAN;
                break;
            case BetCode.Q5_MAX:
                relkey = BetCode.Q5_MIN;
                break;
            case BetCode.Q5_MIN:
                relkey = BetCode.Q5_MAX;
                break;
            default:
                relkey = null;
        }
        return relkey;
    }


    private Map<String, Integer> getBigLogMap(List<OpenCode> codelist) {
        Map<String, Integer> codelong = new HashMap<>();
        for (OpenCode code : codelist) {
            if (code.getOnenum() > 4) {
                codelong = changeMap(codelong, BetCode.Q1_MAX, BetCode.Q1_MIN);
            } else {
                codelong = changeMap(codelong, BetCode.Q1_MIN, BetCode.Q1_MAX);
            }
            if (code.getOnenum() % 2 == 0) {
                codelong = changeMap(codelong, BetCode.Q1_SHUANG, BetCode.Q1_DAN);
            } else {
                codelong = changeMap(codelong, BetCode.Q1_DAN, BetCode.Q1_SHUANG);
            }


            if (code.getTwonum() > 4) {
                codelong = changeMap(codelong, BetCode.Q2_MAX, BetCode.Q2_MIN);
            } else {
                codelong = changeMap(codelong, BetCode.Q2_MIN, BetCode.Q2_MAX);
            }
            if (code.getTwonum() % 2 == 0) {
                codelong = changeMap(codelong, BetCode.Q2_SHUANG, BetCode.Q2_DAN);
            } else {
                codelong = changeMap(codelong, BetCode.Q2_DAN, BetCode.Q2_SHUANG);
            }

            if (code.getThridnum() > 4) {
                codelong = changeMap(codelong, BetCode.Q3_MAX, BetCode.Q3_MIN);
            } else {
                codelong = changeMap(codelong, BetCode.Q3_MIN, BetCode.Q3_MAX);
            }
            if (code.getThridnum() % 2 == 0) {
                codelong = changeMap(codelong, BetCode.Q3_SHUANG, BetCode.Q3_DAN);
            } else {
                codelong = changeMap(codelong, BetCode.Q3_DAN, BetCode.Q3_SHUANG);
            }


            if (code.getFounum() > 4) {
                codelong = changeMap(codelong, BetCode.Q4_MAX, BetCode.Q4_MIN);
            } else {
                codelong = changeMap(codelong, BetCode.Q4_MIN, BetCode.Q4_MAX);
            }
            if (code.getFounum() % 2 == 0) {
                codelong = changeMap(codelong, BetCode.Q4_SHUANG, BetCode.Q4_DAN);
            } else {
                codelong = changeMap(codelong, BetCode.Q4_DAN, BetCode.Q4_SHUANG);
            }

            if (code.getFivnum() > 4) {
                codelong = changeMap(codelong, BetCode.Q5_MAX, BetCode.Q5_MIN);
            } else {
                codelong = changeMap(codelong, BetCode.Q5_MIN, BetCode.Q5_MAX);
            }
            if (code.getFivnum() % 2 == 0) {
                codelong = changeMap(codelong, BetCode.Q5_SHUANG, BetCode.Q5_DAN);
            } else {
                codelong = changeMap(codelong, BetCode.Q5_DAN, BetCode.Q5_SHUANG);
            }


        }
        return codelong;
    }

    private Map<String, Integer> changeMap(Map<String, Integer> codelong, String oneda, String onexiao) {
        if (codelong.get(oneda) != null) {
            codelong.put(oneda, codelong.get(oneda) + 1);
        } else {
            codelong.put(oneda, 1);
            codelong.remove(onexiao);
        }
        return codelong;
    }

    /**
     * 获取开奖结果
     *
     * @return
     */
    private List<OpenCode> getopencode() {
        List<OpenCode> codelist = new ArrayList<OpenCode>();
        try {
            LinkedHashMap opencodeStr = restTemplate.getForObject(openresult, LinkedHashMap.class, "");
            LinkedHashMap period = (LinkedHashMap) opencodeStr.get("period");
            List<LinkedHashMap> rows = (List<LinkedHashMap>) period.get("row");
            if (StringUtils.isEmpty(opencodeStr)) return null;

            for (LinkedHashMap obj : rows) {
                String opencode = obj.get("opencode").toString();
                if (StringUtils.isEmpty(opencode)) {
                    continue;
                } else {
                    String pid = obj.get("pid").toString();
                    OpenCode code = new OpenCode(opencode, pid);
                    codelist.add(code);
                }
            }
            Collections.sort(codelist);
            return codelist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return codelist;
    }
}
